package com.fl.queue;

import com.alipay.api.AlipayApiException;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.response.AlipayTradeCloseResponse;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.fl.config.AlipayConfigInfo.alipayClient;

@Component
public class DelayConsumer {

    //延时队列实现订单超时关闭支付
    @RabbitListener(bindings = {@QueueBinding(value = @Queue(value = "direct.delay.queue"),exchange = @Exchange(value = "direct.delay.exchange"),key = {"DelayKey"})})
    public void counsumer(Channel channel, Message message) throws Exception {
        System.out.println(message.getBody());
        AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
        request.setBizContent("{" +
                "\"out_trade_no\":\"32\"" +
                "  }");
        AlipayTradeCloseResponse response = alipayClient.execute(request);
        System.out.println("成功");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new Date());
        System.out.println("关闭订单当前时间："+format);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
